package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.system.model.dto.DictionaryDTO;
import com.xuecheng.system.convert.DictionaryConvert;
import com.xuecheng.system.entity.Dictionary;
import com.xuecheng.system.mapper.DictionaryMapper;
import com.xuecheng.system.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {


    /**
     * 查询全部数据字典信息(常量信息-便于前端数据查询后的持久化操作)
     *
     * @return List<DictionaryDTO>
     */
    @Override
    public List<DictionaryDTO> findAll() {
        //1.查询全部数据
        List<Dictionary> dicList = this.list();
        //2.将POs转为DTOs并返回
        List<DictionaryDTO> dtoList = DictionaryConvert.INSTANCE.entitys2dtos(dicList);
        return dtoList;
    }

    /**
     * 根据 Code 查询数据字典
     *
     * @param code {@link String}
     * @return DictionaryDTO 数据传输对象
     */
    @Override
    public DictionaryDTO getDicByCode(String code) {
        //1.判断数据的合法性
        if (StringUtils.isBlank(code)) {
            throw new RuntimeException("code不能为空");
        }
        //创建查询条件
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        //根据code查询
        queryWrapper.eq(Dictionary::getCode, code);
        Dictionary dictionary = this.getOne(queryWrapper);
        //将查询到的数据并且转换为dtos集合
        DictionaryDTO dictionaryDTO = DictionaryConvert.INSTANCE.entity2dto(dictionary);

        return dictionaryDTO;
    }
}
